package engine.newUi
{
	import engine.control.NxTablePageTurner;
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.logic.MenuListManager;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	
	import flash.events.MouseEvent;
	
	import newx.com.control.table.NxTableEvent;
	import newx.com.control.window.NxWindowEvent;

	public class PkListRankLogic
	{
		private static var _self	:PkListRankLogic
		private var _myDis			:PkListRankUI;
		
		private var _model			:ModelLocator;
		
		private var _tableTurn:NxTablePageTurner
		public function PkListRankLogic()
		{
			init()
		}
		public static function getInstance():PkListRankLogic
		{
			if(_self	==	null)
			{
				_self	=	new PkListRankLogic()
			}
			return _self;
		}
		/**
		 * 初始化 
		 */		
		private function init():void
		{
			_myDis	=	PkListRankUI.getInstance();
			_model	=	ModelLocator.getInstance();
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_SEND_CLONE_CHALLENGE_TOP),showPKlistInfo);
			_tableTurn	=	new NxTablePageTurner(_myDis.preBtn,_myDis.nextBtn,_myDis.pkListInfo,_myDis.numLabel);
		}
		/**
		 * 显示排行榜信息 
		 * @param e
		 * wstring	name		名字
		 * int		level		等级
		 * int		sex			性别
		 * int		fightpower	战力
		 * int		trend		趋势(0:下降 1:上升)
		 * int		online		在线状态(0:离线 1:在线)
		 */		
		private function showPKlistInfo(e:CustomMsgEvent):void
		{
			var index:int		=	1;
			_myDis.pkListInfo.dataProvider	=	null;
			for(var i:int = 0; i < e.pList.length; i+= 6)
			{
				var itemObj:Object	=	{}
				itemObj["name"]			=	e.pList[i];
				itemObj["level"]		=	e.pList[i + 1];
				itemObj["sex"]			=	e.pList[i + 2];
				itemObj["fightpower"]	=	e.pList[i + 3]
				if(e.pList[i + 4] == 2)
				{
					itemObj["trend"]		=	"<font color = '#808080'>￣</font>";
				}
				else
				{
					itemObj["trend"]		=	e.pList[i + 4]> 0?"<font color = '#00FF00'>↑</font>":"<font color = '#FF0000'>↓</font>";
				}
				
				itemObj["online"]		=	e.pList[i + 5];
				if(itemObj["online"] > 0)
				{
					_myDis.pkListInfo.addLineByIndex([
						"<font color = '#FFFFFF'>" + index 													+ "</font>",
						"<font color = '#FFFFFF'>" + ((itemObj["sex"] > 1?"♀":"♂") + itemObj["name"])		+ "</font>",
						"<font color = '#FFFFFF'>" + itemObj["level"] 										+ "</font>",
						"<font color = '#FFFFFF'>" + itemObj["fightpower"] 								+ "</font>",
						"<font color = '#FFFFFF'>" + itemObj["trend"] 										+ "</font>"
					],-1,itemObj);
				}
				else
				{
					_myDis.pkListInfo.addLineByIndex([
						"<font color = '#666666'>" + index 													+ "</font>",
						"<font color = '#666666'>" + ((itemObj["sex"] > 1?"♀":"♂") + itemObj["name"])		+ "</font>",
						"<font color = '#666666'>" + itemObj["level"] 										+ "</font>",
						"<font color = '#666666'>" + itemObj["fightpower"] 								+ "</font>",
						"<font color = '#666666'>" + itemObj["trend"] 										+ "</font>"
					],-1,itemObj);
				}
				index ++;
			}
			_tableTurn.onPageDis();
		}
		/**
		 * 表格点击 
		 * @param e
		 * 
		 */		
		private function tableClickHandle(e:NxTableEvent):void
		{
			if(e.lineArrData ==null)return;
			var playName:String	=	e.lineArrData[0]["data"]["name"];
			if(playName != _model.scene.Role.getPlayerName())
			{
				MenuListManager.clickPkListName(playName);
			}

		}
		/**
		 * 关闭窗口
		 * @param e
		 * 
		 */		
		private function clickHandle(e:NxWindowEvent):void
		{
			_myDis.hide();
		}
		/**
		 * 添加事件
		 */		
		public function addListener():void
		{
			_myDis.pkListInfo	.addEventListener(NxTableEvent.TABLE_CLICK,	tableClickHandle);
			_myDis.windowBG		.addEventListener(NxWindowEvent.WINDOW_CLOSE,				clickHandle);
			_tableTurn			.addEventListener();
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_ENTRY_GET_CLONE_PLAYER_TOP)
		}
		/**
		 * 移除事件 
		 */		
		public function removeListener():void
		{
			_tableTurn			.removeEventListener()
			_myDis.windowBG		.removeEventListener(NxWindowEvent.WINDOW_CLOSE,			clickHandle);
			_myDis.pkListInfo	.removeEventListener(NxTableEvent.TABLE_CLICK,	tableClickHandle);
		}
	}
}